package controller;

import game.Piece;
import game.PieceType;

import java.awt.*;

/**
 * This class represents the board on which checkers is being played. The board
 * holds a collection of pieces.
 * 
 * @invariant all variables have valid values
 * 
 * @author
 */
public class Board {

	private Piece board[]; // the pieces that are on the board

	/**
	 * This constructor creates a new board at the beginning of the game
	 */

	public Board() {
		board = new Piece[64];

		// sets up red peices
		for (int i = 0; i < 24; i = i + 2) {
			board[i] = new Piece(Color.red, PieceType.SINGLE);
		}
		// makes the black pieces
		for (int i = 42; i < 64; i = i + 2) {
			board[i] = new Piece(Color.black, PieceType.SINGLE);
		}
	}

	/**
	 * Move the piece at the start position to the end position
	 * 
	 * @param start
	 *            - current location of the piece
	 * @param end
	 *            - the position where piece is moved
	 * 
	 * @return -1 if there is a piece in the end position
	 */
	public int movePiece(int start, int end) {

		int returnValue = 0;

		// check if the end position of the piece is occupied
		if (occupied(end)) {

			// if it is return -1
			returnValue = -1;

			// if it is not set a start position in the array to null
			// put a piece object at the end position in the array
			// that was at the start positon before
		} else {

			board[end] = board[start];
			board[start] = null;

		}

		return returnValue;

	}

	/**
	 * This method checks if the space on the board contains a piece
	 * 
	 * @param space
	 *            - the space that needs to be checked
	 * 
	 * @return true or false depending on the situation
	 */
	public boolean occupied(int space) {
		if (board[space] != null) {
			return true; // its occupied
		} else
			return false; // the space is not occupied
	}

	/**
	 * This method removes piece at the position space
	 * 
	 * @param space
	 *            - the positon of the piece to be removed
	 */
	public void removePiece(int space) {
		board[space] = null;
	}

	/**
	 * This method creates a king piece
	 * 
	 * @param space
	 *            - the psotion at which the king piece is created
	 */
	public void kingPiece(int space) {

		// create a new king piece
		// go to the space position in the array and place it there
		// if the position is not ocupied
		Color color = board[space].getColor();
		Piece piece = new Piece(color, PieceType.KING);
		board[space] = piece;

	}

	/**
	 * This method returns the color of the piece at a certain space
	 * 
	 * @param space
	 *            - the position of the piece on the board
	 * 
	 * @return the color of this piece
	 */
	public Color colorAt(int space) {
		if (occupied(space)) {
			return board[space].getColor();
		}
		return null;
	}

	/**
	 * This method returns the piece at the certain position
	 * 
	 * @param space
	 *            - the space of the piece
	 * 
	 * @return the piece at that space
	 */
	public Piece getPieceAt(int space) {
		return board[space];
	}

	/**
	 * This method returns if there is a piece of color on the board
	 * 
	 * @param color
	 *            - the color of the piece
	 * 
	 * @return true if there is a piece of color left on the board else return
	 *         false
	 */
	public boolean hasPieceOf(Color color) {
		for (int i = 0; i < 64; i = i + 2) {
			if (board[i].getColor() == color)
				return true;
		}
		return false;
	}

	/**
	 * This method returns the size of the board
	 * 
	 * @return the size of the board
	 */
	public int sizeOf() {
		return board.length;
	}

}
